package com.jjz.id.common.cipher;

import java.nio.charset.StandardCharsets;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * @author ZJL
 * @create 2025-05-28-10:42
 */

public class RsaCipher implements PasswordCipher {
    RSA rsa;

    public RsaCipher(String privateKey, String publicKey) {
        this.rsa = new RSA(privateKey, publicKey);
    }

    public String encrypt(String password) {
        byte[] bytes = this.rsa.encrypt(password, KeyType.PublicKey);
        return Base64Encoder.encode(bytes);
    }

    public String decrypt(String password) {
        byte[] bytes = Base64Decoder.decode(password.getBytes(StandardCharsets.UTF_8));
        byte[] decrypt = this.rsa.decrypt(bytes, KeyType.PrivateKey);
        return StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
    }

}